﻿using Lima.ResumeSys.DAL;
using Lima.ResumeSys.Models;
using Lima.ResumeSys.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lima.ResumeSys
{
    public partial class OrgForm : AntdUI.Window
    {
        private string _Id = "";
        private bool result = false;
        private string _CurrentOrgId = "";
        OrganizationDAL orgdal = new OrganizationDAL();
        public OrgForm(string id, string currentOrgId)
        {
            _Id = id;
            _CurrentOrgId = currentOrgId;
          
            InitializeComponent();
            BindDropDownTreeViewData();
            GetModel();
        }
        /// <summary>
        /// 绑定下拉框组织数据
        /// </summary>
        private void BindDropDownTreeViewData()
        {
            var dataTable = orgdal.GetTreeViewData();

            var allData = dataTable.AsEnumerable().Select(row => new Organization
            {
                ID = row["Id"].ToString(),
                Code = row["Code"].ToString(),
                OrgName = row["OrgName"].ToString(),
                ParCode = row["ParCode"].ToString(),
            }).ToList();

            TreeBuilder treeBuilder = new TreeBuilder();
            treeBuilder.BindDropDownWithParentId(orgSelect, allData, _CurrentOrgId);
        }
        private void txtSort_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void GetModel()
        {
            if (!string.IsNullOrWhiteSpace(_Id))
            {
                Organization entity = orgdal.GetModel(_Id);
                txtCode.Text = entity.Code;
                txtName.Text = entity.OrgName;
                txtSort.Text = entity.Sort.ToString();
                Organization parEntity = orgdal.GetModel(entity.ParCode);
                orgSelect.SelectedText = parEntity.OrgName;
                orgSelect.SelectedValue = entity.ParCode;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            result = false;
            if (string.IsNullOrWhiteSpace(txtCode.Text) ||
             string.IsNullOrWhiteSpace(txtName.Text))
            {
                MessageBox.Show("信息输入不完整,无法添加！带*为必填项", "提示", MessageBoxButtons.OK);
            }
            else
            {
                Organization entity = new Organization();
                entity.Code = txtCode.Text;
                entity.OrgName = txtName.Text;
                entity.Sort= int.Parse(txtSort.Text);
                entity.ParCode=orgSelect.SelectedValue.ToString();

                if (string.IsNullOrWhiteSpace(_Id))
                {
                    entity.ID = Guid.NewGuid().ToString();
                    result = orgdal.Add(entity);
                }
                else
                {
                    entity.ID = _Id;
                    result = orgdal.Update(entity);
                }
                if (result)
                {
                    AntdUI.Message.success(this, "操作成功！", autoClose: 3);
                    
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else {
                    AntdUI.Message.error(this, "操作失败！", autoClose: 3);
                }

            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
